//
//  PKRadioMainDetailController.m
//  AMomentApp
//
//  Created by lanou on 15/10/4.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKRadioMainDetailController.h"
#import "PKRequestManager.h"
#import "PKRadioDetailModel.h"
#import "PKRadioDetailCell.h"
#import "PKPlayViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
@interface PKRadioMainDetailController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *radioDetailTabView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) NSMutableArray *radioArray;
@property (nonatomic, strong) NSMutableDictionary *headerDic;
@property (nonatomic, assign) int start;

@end

@implementation PKRadioMainDetailController

-(NSMutableArray *)radioArray
{
    if (_radioArray == nil) {
        self.radioArray = [NSMutableArray array];
    }
    return _radioArray;
}

- (void)configureData
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",self.start],@"start",@"",@"auth",@"1",@"client",@"63A94D37-33F9-40FF-9EBB-481182338873",@"deviceid",self.radioId,@"radioid",@"3.0.4",@"version", nil];
    NSString *urlString = @"http://api2.pianke.me/ting/radio_detail";
    if (self.start >= 10) {
        urlString = @"http://api2.pianke.me/ting/radio_detail_list";
    }
    [PKRequestManager PKRequestManagerrequsetWithType:POST URLString:urlString ParDIc:dic finish:^(NSData *data) {
        [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        _headerDic = dic[@"data"][@"radioInfo"];
        for (NSDictionary *detailDic in dic[@"data"][@"list"]) {
            PKRadioDetailModel *radioModel = [[PKRadioDetailModel alloc]init];
            [radioModel setValuesForKeysWithDictionary:detailDic];
            [self.radioArray addObject:radioModel];
        }
        [self showRadioDetailTabHeaderView];
        [self.radioDetailTabView.footer endRefreshing];
        [_radioDetailTabView reloadData];
    } error:^(NSError *error) {
        NSLog(@"%@",[error localizedDescription]);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _start = 0;
    _headerDic = [NSMutableDictionary dictionary];
    [self configureData];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 25, SCREEN_WIDTH-60, 30)];
    label.text = self.string;
    [self.view addSubview:label];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(10, 28, 25, 25);
    [self.backButton setImage:[UIImage imageNamed:@"returnreturn.png"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(getBackRadioDetailControl:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backButton];
    
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH-100)];
    
    _radioDetailTabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70) style:UITableViewStylePlain];
    _radioDetailTabView.delegate = self;
    _radioDetailTabView.dataSource = self;
    _radioDetailTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _radioDetailTabView.rowHeight = 70;
    _radioDetailTabView.tableHeaderView = _headerView;
    [_radioDetailTabView registerClass:[PKRadioDetailCell class] forCellReuseIdentifier:@"radioDeatil"];
    [self.view addSubview:_radioDetailTabView];
    
    __weak PKRadioMainDetailController *radioMainDVC = self;
    self.radioDetailTabView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        radioMainDVC.start += 10;
        [self configureData];
    }];
    // Do any additional setup after loading the view.
}

- (void)getBackRadioDetailControl:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)showRadioDetailTabHeaderView
{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH-200)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.headerDic[@"coverimg"]]];
    [self.headerView addSubview:imageView];
    UIImageView *iconView = [[UIImageView alloc]initWithFrame:CGRectMake(20, SCREEN_WIDTH - 180, 30, 30)];
    iconView.layer.cornerRadius = 15;
    [iconView sd_setImageWithURL:[NSURL URLWithString:self.headerDic[@"userinfo"][@"icon"]]];
    [self.headerView addSubview:iconView];
    
    UILabel *uname = [[UILabel alloc]initWithFrame:CGRectMake(60, SCREEN_WIDTH-180, SCREEN_WIDTH-100, 30)];
    uname.font = [UIFont systemFontOfSize:15];
    uname.text = self.headerDic[@"userinfo"][@"uname"];
    [self.headerView addSubview:uname];
    
    UILabel *descLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, SCREEN_WIDTH-140, SCREEN_WIDTH-100, 30)];
    descLabel.font = [UIFont systemFontOfSize:15];
    descLabel.text = self.headerDic[@"desc"];
    [self.headerView addSubview:descLabel];
    
    UILabel *visitLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-90, SCREEN_WIDTH-180, 90, 30)];
    visitLabel.text = [NSString stringWithFormat:@"%@",self.headerDic[@"musicvisitnum"]];
    [self.headerView addSubview:visitLabel];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.radioArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKRadioDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radioDeatil" forIndexPath:indexPath];
    cell.radioDetailModel = self.radioArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKPlayViewController *playViewControl = [[PKPlayViewController alloc]init];
    for (int i =0; i < self.radioArray.count; i++) {
        PKRadioDetailModel *model = self.radioArray[i];
        if (i == indexPath.row) {
            model.isSelected = YES;
        }else
        {
            model.isSelected = NO;
        }
    }
    playViewControl.dataSourceArray = self.radioArray;
    for (PKRadioDetailModel *model in self.radioArray) {
        [playViewControl.musicURLArray addObject:model.musicUrl];
    }
    playViewControl.tingId = ((PKRadioDetailModel *)self.radioArray[indexPath.row]).tingid;
    playViewControl.index = indexPath.row;
    [self.navigationController pushViewController:playViewControl animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
